import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/login')
  },
  {
    path: '/layout',
    name: 'layout',
    component: () => import(/* webpackChunkName: "about" */ '../views/layout'),
    meta: {
      needLogin: true,
    },
    children: [
      {
        path: '/layout',
        redirect: '/layout/home'
      },
      {
        path:'home',
        component:  () => import(/* webpackChunkName: "about" */ '../views/layout/home.vue'),
        children: [
          {
            path: '/layout/home',
            redirect: '/layout/home/swiper1'
          },
          {
            path: "swiper1",
            component:  () => import(/* webpackChunkName: "about" */ '../views/layout/pages/swiper1.vue'),
          },
          {
            path: "swiper2",
            component:  () => import(/* webpackChunkName: "about" */ '../views/layout/pages/swiper2.vue'),
          }
        ]
      },
      {
        path:'list',
        component:  () => import(/* webpackChunkName: "about" */ '../views/layout/list.vue'),
      },
      {
        path:'cart',
        component:  () => import(/* webpackChunkName: "about" */ '../views/layout/cart.vue'),
      },
      {
        path:'my',
        component:  () => import(/* webpackChunkName: "about" */ '../views/layout/my.vue'),
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next) => {
  if(to.meta.needLogin) {
    if(window.sessionStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router
